package com.zbkj.crmeb.store.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.CommonPage;
import com.common.PageParamRequest;
import com.crmeb.core.exception.CrmebException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.utils.DateUtil;
import com.zbkj.crmeb.store.dao.ActivityManageDao;
import com.zbkj.crmeb.store.model.ActivityManage;
import com.zbkj.crmeb.store.model.ProductEnroll;
import com.zbkj.crmeb.store.request.ActivityManageRequest;
import com.zbkj.crmeb.store.response.ActivityManageResponse;
import com.zbkj.crmeb.store.service.ActivityManageService;
import com.zbkj.crmeb.store.service.ActivityProductService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class ActivityManageServiceImpl extends ServiceImpl<ActivityManageDao, ActivityManage> implements ActivityManageService {
    @Resource
    private ActivityManageDao activityManageDao;
    @Autowired
    private ActivityProductService activityProductService;

    /**
     * 分页查询
     *
     * @param @param request pageParamRequest
     * @return PageInfo<ActivityManageResponse>
     * @Description:
     * @author duyb
     * @Date: 2022-11-10
     */
    @Override
    public PageInfo<ActivityManageResponse> pageEntity(ActivityManageRequest request, PageParamRequest pageParamRequest) {
        Page<Object> page = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<ActivityManage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(request.getActivityName())) {
            lambdaQueryWrapper.like(ActivityManage::getActivityName, request.getActivityName());
        }
        if (request.getIsShow() != null) {
            lambdaQueryWrapper.eq(ActivityManage::getIsShow, request.getIsShow());
        }
        if (!Objects.isNull(request.getStatus())){
            switch (request.getStatus()) {
                case 1:
                    lambdaQueryWrapper.gt(ActivityManage::getActivityStartTime, new Date());
                    break;
                case 2:
                    lambdaQueryWrapper.and(
                            a -> a.eq(ActivityManage::getActivityType, true)
                                    .or(b -> b.lt(ActivityManage::getActivityStartTime, new Date())
                                            .gt(ActivityManage::getActivityEndTime, new Date())
                                    ));
                    break;
                case 3:
                    lambdaQueryWrapper.lt(ActivityManage::getActivityEndTime, new Date());
                    break;
            }
        }

        lambdaQueryWrapper.orderByAsc(ActivityManage::getSort);
        lambdaQueryWrapper.orderByAsc(ActivityManage::getCreateTime);
        List<ActivityManage> activityManageList = this.list(lambdaQueryWrapper);
        List<ActivityManageResponse> activityManageResponseList = new ArrayList<>();

        //处理JSONObject
        if (CollectionUtils.isNotEmpty(activityManageList)) {
            for (ActivityManage activityManage : activityManageList) {
                ActivityManageResponse activityManageResponse = new ActivityManageResponse();
                BeanUtils.copyProperties(activityManage, activityManageResponse);
                if (activityManage.getActivityType()){
                    activityManageResponse.setStatus(2);
                }else if (DateUtil.isEffectiveDate(new Date(),activityManage.getActivityStartTime(),activityManage.getActivityEndTime())){
                    activityManageResponse.setStatus(2);
                }else if (DateUtil.compareDate(activityManage.getActivityStartTime(),new Date())){
                    activityManageResponse.setStatus(1);
                }else {
                    activityManageResponse.setStatus(3);
                }
                activityManageResponseList.add(activityManageResponse);
            }
        }

        return CommonPage.copyPageInfo(page, activityManageResponseList);
    }

    /**
     * 通过主键查询
     *
     * @param @param id
     * @return ActivityManage
     * @Description:
     * @author duyb
     * @Date: 2022-11-10
     */
    @Override
    public ActivityManage getEntity(Integer id) {
        return this.getById(id);
    }


    /**
     * 新增/修改
     *
     * @param @param activityManageRequest
     * @Description:
     * @author duyb
     * @Date: 2022-11-10
     */
    @Override
    public Boolean saveEntity(ActivityManageRequest activityManageRequest) {
        //验证参数合法性
        if (StringUtils.isBlank(activityManageRequest.getActivityName())) {
            throw new CrmebException("活动名称不能为空!");
        }

        if (activityManageRequest.getActivityType() == null) {
            throw new CrmebException("活动类型不能为空!");
        }else if(!activityManageRequest.getActivityType()&&
                (ObjectUtil.isNull(activityManageRequest.getActivityStartTime())||ObjectUtil.isNull(activityManageRequest.getActivityEndTime()))){
            throw new CrmebException("请选择时间段!");
        }

        if (activityManageRequest.getEnrollType() == null) {
            throw new CrmebException("报名类型不能为空!");
        }else if(!activityManageRequest.getEnrollType()&&
                (ObjectUtil.isNull(activityManageRequest.getEnrollStartTime())||ObjectUtil.isNull(activityManageRequest.getEnrollEndTime()))){
            throw new CrmebException("请选择时间段!");
        }
        if (StringUtils.isBlank(activityManageRequest.getIcon())) {
            throw new CrmebException("活动icon不能为空!");
        }
        if (StringUtils.isBlank(activityManageRequest.getContent())) {
            throw new CrmebException("活动介绍不能为空!");
        }
        //排序默认999
        if (activityManageRequest.getSort() == null) {
            activityManageRequest.setSort(999);
        } else if (activityManageRequest.getSort() > 999) {
            throw new CrmebException("排序号最大为999!");
        }
        if (null == activityManageRequest.getId()) {
            activityManageRequest.setCreateTime(new Date());
        }else {
            ActivityManage activity = this.getById(activityManageRequest.getId());
            if (!Objects.equals(activity.getActivityName(), activityManageRequest.getActivityName())){
                activityProductService.update(new UpdateWrapper<ProductEnroll>().lambda()
                        .eq(ProductEnroll::getActivityId, activityManageRequest.getId())
                        .set(ProductEnroll::getActivityName, activityManageRequest.getActivityName())
                );
            }
        }
        ActivityManage activityManage = new ActivityManage();
        BeanUtils.copyProperties(activityManageRequest, activityManage);

        activityManage.setUpdateTime(new Date());

        return this.saveOrUpdate(activityManage);
    }


}
